﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using Wuqi.Webdiyer;

using DreamBuilder.Common;
using xHubble.SingleWell.DataAccess;
using ePlanet.ArchiveMange.Services;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveMange.Models;


namespace Web.Views.ArchiveFlowView
{
    public partial class ArchiveSearch : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            aspNetPagerSearch.PageSizeChanging += new EventHandler(pagerTransfered_PageSizeChanging);
        }

        protected void pagerTransfered_PageSizeChanging(object sender, EventArgs e)
        {
            SearchData();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            this.aspNetPagerSearch.CurrentPageIndex = 1;
            SearchData();
        }

        
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearchByArchiveNum_Click(object sender, EventArgs e)
        {
            this.aspNetPagerSearch.CurrentPageIndex = 1;
            SearchDataByArchiveNum();
        }

        private void SearchData()
        {
            try
            {
                string searchKeywords = this.txtSearchKeyword.Value;
                if (searchKeywords == "- 可输入档案元素信息查询 -")
                    searchKeywords = "";
                PageInfo pageinfo = new PageInfo();
                pageinfo.PageIndex = this.aspNetPagerSearch.CurrentPageIndex;
                pageinfo.PageSize = this.aspNetPagerSearch.PageSize;
                DataTable archiveTable = ArchiveDAL.SearchArchiveData(int.Parse(this.ddlArchiveType.SelectedValue), searchKeywords, pageinfo);
                gv_ArchiveList.DataSource = archiveTable;
                gv_ArchiveList.DataBind();
                this.aspNetPagerSearch.RecordCount = pageinfo.RecordCount;
            }
            catch (Exception ex)
            {
                Alert("系统异常，请联系管理员！");
            }
            
        }

        private void SearchDataByArchiveNum()
        {
            try
            {
                string archiveNum = this.txtSearchKeyword.Value.Trim();
                if (archiveNum == "- 可输入档案元素信息查询 -")
                    archiveNum = "";

                DataTable table = ArchiveDAL.SearchArchiveData(int.Parse(this.ddlArchiveType.SelectedValue), archiveNum);
                gv_ArchiveList.DataSource = table;
                gv_ArchiveList.DataBind();
                this.aspNetPagerSearch.RecordCount = table == null ? 0 : table.Rows.Count;
            }
            catch (System.Exception ex)
            {
                Alert("系统异常，请联系管理员！");
            }
            
        }

        protected void aspNetPagerSearch_PageChanging(object sender, PageChangingEventArgs e)
        {
            aspNetPagerSearch.CurrentPageIndex = e.NewPageIndex;
            SearchData();
        }

        protected void gv_ArchiveList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    DataRow row = ((DataRowView)e.Row.DataItem).Row;
                    e.Row.Cells[2].Text = string.Format(@"<a href='javascript:void(0)'   onclick=window.open('../ArchiveView/ArchivePreview.aspx?archiveId={0}&archivetype={1}&needcheck=1','newwin') >{2}</a>"
                        , row["dataid"].ToString(), this.ddlArchiveType.SelectedValue, row["title"].ToString());
                   
                }
            }
        }
    }
}